package health.lm.com.mine.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import health.lm.com.component_base.arouter.Router;
import health.lm.com.component_base.base.mvp.activity.XListActivity;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.adapter.CardCenterAdapter;
import health.lm.com.mine.entity.CardCenterEntity;
import health.lm.com.mine.present.CardCenterPresenter;
import health.lm.com.router.ARouterConstant;

import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_LEFT_BUTTON;
import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_RIGHT_TEXT;
import static health.lm.com.component_base.arouter.Router.WebActivity;

/**
 * 卡券中心
 *
 * @author GQI
 */
@Route(path = ARouterConstant.CardCenterActivity)
public class CardCenterActivity extends XListActivity<CardCenterPresenter> {
    @BindView(R2.id.title_bar)
    CommonTitleBar titlebar;
    @BindView(R2.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R2.id.rlv_card)
    RecyclerView rlvCard;
    private List<CardCenterEntity.Union_list> beanList;
    private CardCenterAdapter adapter;
    private boolean isRefresh;
    private TextView tvCount, tvRanking;

    @Override
    public int getLayoutId() {
        return R.layout.activity_card_center;
    }

    @Override
    public CardCenterPresenter newP() {
        return new CardCenterPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initAdapter();
        super.rlRefreshLayout = srlLayout;
        super.adapter = adapter;
        super.recyclerView = rlvCard;
        super.initData(savedInstanceState);
        titlebar.setListener((v, action, extra) -> {
            if (action == ACTION_LEFT_BUTTON) {
                finish();
            }else if(action == ACTION_RIGHT_TEXT){
                withValueActivity(ARouterConstant.InfoWebviewActivity).withString(ARouterConstant.TOPBAR_TITLE,"使用说明").withInt(ARouterConstant.TYPE, ARouterConstant.CARD_INFO).navigation();
            }
        });
    }

    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        this.isRefresh = isRefresh;
        getP().getListMore(isRefresh, layout, page, pageSize);
    }


    /**
     * 初始化适配器
     */
    private void initAdapter() {
        beanList = new ArrayList<>();
        adapter = new CardCenterAdapter(beanList);
        rlvCard.setLayoutManager(new LinearLayoutManager(mActivity));
        rlvCard.setAdapter(adapter);
    }

    public void getListSuccess(List<CardCenterEntity.Union_list> beanList) {
        this.beanList = beanList;
        if (beanList!=null && isRefresh && beanList.size() >= pageSize) {
            initLoadMore();
        }
        notifyData();
    }


    private void notifyData() {
        if (beanList == null) {
            return;
        }
        if (beanList.size() < pageSize) {
            adapter.loadMoreEnd(false);
        } else {
            adapter.loadMoreComplete();
        }
        if (isRefresh) {
            adapter.setNewData(beanList);
        } else {
            adapter.addData(beanList);
        }
        if (beanList.size()<=0){
            adapter.setEmptyView(empty());
        }
    }

}
